//
//  TGVisitorView.swift
//  TGWeiBo
//
//  Created by 涛哥无敌威武 on 16/4/17.
//  Copyright © 2016年 taoge. All rights reserved.
//

import UIKit
import SnapKit
class TGVisitorView: UIView {

    //定义一个闭包
    var visitorClosure:(() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
     //MARK: - 供外部调用的方法
    func setVisitorViewInfo(imageName:String?, title:String?){
        //消息  发现  我
        if let img = imageName,tit = title{
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            //隐藏圆圈
            circleImageView.hidden = true
        
        }else{  // 首页,这里调用圆圈转起来的方法
        
            setCircleAnim()
        }
    }
    
    
    //圆圈转起来的方法
    private func setCircleAnim(){
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 15
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        //当切换tabbar或者程序退到后台,默认是动画移除的,这里取消移除
        anim.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(anim, forKey: nil)
        
    
    
    }
    
    
    //MARK: 设置视图
    private func setupUI(){
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        //1.添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //2.添加约束
        //2.1圆圈
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        //2.2遮盖
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
         //2.3小房子
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
         //2.4messageLabel
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
            make.height.equalTo(36)
            make.width.equalTo(230)
        }
        //2.5 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        //2.6 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
    }
    
    
    //MARK: 懒加载控件
    //圆圈
    private lazy var circleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮挡
    private lazy var maskImageView:UIImageView = {
    
        let image = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
        return image
    
        }()
        //小房子
    private lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文字
    private lazy var messageLabel:UILabel = {
    
        let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        lab.numberOfLines = 0
        lab.font = UIFont.systemFontOfSize(14)
        lab.textAlignment = .Center
        
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return lab
        
        }()
    
    //注册按钮
    private lazy var registerButton:UIButton = {
    
        let btn = UIButton()
        //点击事件
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        //背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //添加title
        btn.setTitle("注册", forState: UIControlState.Normal)
        //设置颜色
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        //设置字体大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
    
        return btn
    }()
    
    //登录按钮
    private lazy var loginButton:UIButton = {
    
        let btn = UIButton()
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        return btn
    }()
    
   @objc private func loginClick(){
    
    visitorClosure?()
    
    }

}
